package com.leetcode.offer.chapter3;

import com.leetcode.basic.TreeNode;

/**
 * @author Dennis Li
 * @date 2020/7/13 15:48
 */
public class IsSubStructure_26 {

    // 树递归的核心在于如何通过空指针来退出循环
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if (A == null || B == null) return false;
        // 经典的结构体
        return sub(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
    }

    public boolean sub(TreeNode A, TreeNode B) {
        if (B == null) return true;
        // B不为null A为null，那么返回false
        if (A == null) return false;
        return A.val == B.val && sub(A.left, B.left) && sub(A.right, B.right);
    }

}
